#include "MainForm.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;

MainForm::MainForm(void)
{
}

MainForm::~MainForm(void)
{
}

bool
MainForm::Initialize()
{
	// Construct an XML form
	Construct(L"IDF_MAINFORM");

	return true;
}

result
MainForm::OnInitializing(void)
{
	result r = E_SUCCESS;

	paused = false;

	// Create new Flash control
	__pFlash = new Flash();
	__pFlash->Construct(
			Osp::Graphics::Rectangle(0, 0, 480, 600),
			FLASH_STYLE_NORMAL,
			String(L"/Res/bada.swf"));

	AddControl(*__pFlash);

	// Get a button via resource ID
	__pButtonStart = static_cast<Button *>(GetControl(L"IDC_BUTTON2"));
	if (__pButtonStart != null)
	{
		__pButtonStart->SetActionId(ID_BUTTON_START);
		__pButtonStart->AddActionEventListener(*this);
	}

	__pButtonStop = static_cast<Button *>(GetControl(L"IDC_BUTTON3"));
	if (__pButtonStop != null)
	{
		__pButtonStop->SetActionId(ID_BUTTON_STOP);
		__pButtonStop->AddActionEventListener(*this);
	}

	__pButtonPp = static_cast<Button *>(GetControl(L"IDC_BUTTON1"));
	if (__pButtonPp != null)
	{
		__pButtonPp->SetActionId(ID_BUTTON_PP);
		__pButtonPp->AddActionEventListener(*this);
	}

	return r;
}

result
MainForm::OnTerminating(void)
{
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void MainForm::OnUserEventReceivedN(RequestId requestId, Osp::Base::Collection::IList* pArgs)
{
	switch (requestId) {
		case REQUEST_FLASH_PLAY:
			Draw();
			Show();
			__pFlash->SetFocus();
			__pFlash->Play();
			break;
		case REQUEST_FLASH_STOP:
			__pFlash->Stop();
			break;
		case REQUEST_FLASH_PLAY_PAUSE:
			(paused)? __pFlash->Play(): __pFlash->Pause();
			paused=!paused;
			break;
		default:
			break;
	}
}

void
MainForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	switch(actionId)
	{
		case ID_BUTTON_START:
		{
			SendUserEvent(REQUEST_FLASH_PLAY, null);
		}
		break;
		case ID_BUTTON_STOP:
		{
			SendUserEvent(REQUEST_FLASH_STOP, null);
		}
		break;
		case ID_BUTTON_PP:
		{
			SendUserEvent(REQUEST_FLASH_PLAY_PAUSE, null);
		}
		break;
	default:
		break;
	}
}


